package books4africa

class FulfillmentController {

   def scaffold= true
   
   def inventoryService
   
   def create = {
	   
	   // display the book request details
	   def bookRequest = BookRequest.get(params.id)
	   
	   // look up all books donations in inventory
	   
	   def donations = Donation.executeQuery("select inv from Donation donat, Inventory inv where inv.donation = donat and inv.qty > 0 and donat.book = :book", [book: bookRequest.book])
	   
	   params.max = Math.min(params.max ? params.int('max') : 10, 100)
	   
	   [bookRequestInstance: bookRequest, donationInstanceList: donations, donationInstanceTotal: donations.size()]
   }
   
   def save = {
	   
	   params.each { println it}
	   println params.size()
	  
	   
	   def fulfillment = new Fulfillment(shipQty: params.qtyFilled, donation: params.id, shipDate: new Date()).save()
	   
	   def donation = Donation.get(params.id)
	   
	   inventoryService.removeFromInventory(donation, params.qtyFilled.toInteger())
	   
	   
	   
	   redirect(controller: "BookRequest" , action: "list")
	   
	   
   }
}
